package kit.msc.utility;

import java.awt.CardLayout;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

import kit.msc.gui.MainFrm;

public final class CardLayoutManager {

	public static final String TableCard = "tableCard";
	public static final String ConfigCard = "configCard";

	private static String activeCard = null;
	private static List<String> historyList = new ArrayList<String>();

	public static void switchCard(JPanel panel, JFrame frame, String card) {

		if (frame instanceof MainFrm) {
			// set frame icon
			switch (card) {
			case TableCard:
				frame.setTitle(Preference.get("tableViewName", "TimeTable"));
				frame.setIconImage(Toolkit.getDefaultToolkit().getImage("src/main/resources/icon/application.png"));
				break;
			case ConfigCard:
				frame.setTitle(Preference.get("configViewName", "Configurations"));
				frame.setIconImage(Toolkit.getDefaultToolkit().getImage("src/main/resources/icon/config.png"));
				break;
			default:
				card = TableCard;
				frame.setTitle(Preference.get("tableViewName", "TimeTable"));
				frame.setIconImage(Toolkit.getDefaultToolkit().getImage("src/main/resources/icon/application.png"));
			}

			historyList.add(card);
			activeCard = card;

			CardLayout cardLayout = (CardLayout) panel.getLayout();
			cardLayout.show(panel, card);
		}

	}

	public static void swichtToLeastCard(JPanel panel, JFrame frame) {
		// -1 for that step back and one more -1 for the list size that start
		// counting at 1 instead of 0
		int index = historyList.size() - 2;
		if (index > -1) {
			switchCard(panel, frame, historyList.get(index));
		} else {
			switchCard(panel, frame, historyList.get(historyList.size() - 1));
		}
	}

	public static String getActiveCard() {
		return activeCard;
	}

}
